package org.polyforms.repository.hibernate;

import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.polyforms.repository.jpa.Jpa2IdentifierHelper;
import org.polyforms.repository.jpa.Jpa2QueryParameterResolver;
import org.polyforms.repository.spi.EntityHelper;
import org.polyforms.repository.spi.IdentifierHelper;
import org.polyforms.repository.spi.PersistenceProvider;
import org.polyforms.repository.spi.QueryParameterResolver;

/**
 * Implementation of {@link PersistenceProvider} for Hibernate.
 * 
 * @author Kuisong Tong
 * @since 1.0
 */
@Named
public final class HibernatePersistenceProvider implements PersistenceProvider {
    @PersistenceContext
    private EntityManager entityManager;

    /**
     * {@inheritDoc}
     */
    public boolean matches() {
        return "org.hibernate.impl.SessionImpl".equals(entityManager.getDelegate().getClass().getName());
    }

    /**
     * {@inheritDoc}
     */
    public EntityHelper getEntityHelper() {
        return new HibernateEntityHelper(entityManager);
    }

    /**
     * {@inheritDoc}
     */
    public IdentifierHelper getIdentifierHelper() {
        return new Jpa2IdentifierHelper(entityManager);
    }

    /**
     * {@inheritDoc}
     */
    public QueryParameterResolver getQueryParameterResolver() {
        return new Jpa2QueryParameterResolver();
    }
}
